package com.msb.mall.thirdPart.controller.upload;

import com.msb.common.vo.CommonResult;
import com.msb.mall.thirdPart.service.UploadServiceImpl;
import com.msb.mall.thirdPart.utils.Upload2Server;
import com.msb.mall.thirdPart.vo.UploadServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 直接处理文件的传参
 */
@RestController
@RequestMapping("/thirdPart/simple")
public class SimpleUploadController {

    @Autowired
    private UploadServiceImpl uploadService;

    /**
     * 单文件上传
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    public CommonResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("uploadType") String uploadType) throws Exception{
        UploadServer simpleUploadServer = uploadService.getSimpleUploadServer(uploadType);
        // 这里可以添加文件上传的处理逻辑，例如保存文件到服务器等
        Upload2Server.uploadToServerSimple(file, simpleUploadServer);
        // 返回响应
        return CommonResult.success("文件上传成功");
    }

    /**
     * 多文件上传
     * @param files
     * @return
     */
    @PostMapping("/uploadFiles")
    public CommonResult uploadFiles(@RequestParam("files") MultipartFile[] files, @RequestParam("uploadType") String uploadType) throws Exception{
        UploadServer simpleUploadServer = uploadService.getSimpleUploadServer(uploadType);
        // 处理上传的文件
        for (MultipartFile file : files) {
            // 保存文件逻辑
            Upload2Server.uploadToServerSimple(file, simpleUploadServer);
        }
        return CommonResult.success("文件上传成功");
    }
}
